﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Mimoza.Common;
using Mimoza.DBLayer.Transport;
using Mimoza.Transport;
using Mimoza.DBLayer.Transport.Client;

namespace Mimoza.DBLayer.Transport.Server
{
    public class UpdatesRepository :
        ServerRepository<UpdateRecord, UpdatesRepositoryCustomRequest, IDBUpdateRepository>,
        IDBUpdateRepository
    {
        public UpdatesRepository(IDBUpdateRepository dbUpdates,
            CollectionManager collectionManager, ConnectionList connList)
            : base(dbUpdates, collectionManager, connList)
        {
            UpdatesRepositoryTranferRequest.RegisterInTransport();
            UpdatesRepositoryTranferRequest.Repository = this;
        }

        public UpdateRecord GetUpdate(Guid ID)
        {
            return BaseRepository.GetUpdate(ID);
        }

        public IEnumerable<UpdateRecord> GetUpdates()
        {
            return BaseRepository.GetUpdates();
        }

        override public void Insert(UpdateRecord Update)
        {
            BaseRepository.Insert(Update);
        }

        override public void Update(UpdateRecord Update)
        {
            BaseRepository.Update(Update);
        }

        override public void Delete(UpdateRecord Update)
        {
            BaseRepository.Delete(Update);
        }

        public override TransferObject ProcessRequest(Connection conn, UpdatesRepositoryCustomRequest request)
        {
            TransferObject reply = null;
            switch (request.OperationType)
            {
                case UpdatesRepositoryCustomRequest.CustomOperationType.GetUpdate:
                    reply = GetUpdate(request);
                    break;
                case UpdatesRepositoryCustomRequest.CustomOperationType.GetUpdates:
                    reply = GetUpdates(conn, request);
                    break;
            }

            return reply;
        }

        protected TransferObject GetUpdate(UpdatesRepositoryCustomRequest request)
        {
            UpdateRecord record = GetUpdate(new Guid(request.Argument1));

            return new RepositoryReply<UpdateRecord>(record);
        }

        protected TransferObject GetUpdates(Connection conn, UpdatesRepositoryCustomRequest request)
        {
            IEnumerable<UpdateRecord> enumerator = GetUpdates();

            return RegisterCollection(enumerator, conn);
        }
    }
}
